﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Stickman2D
{
	
	
	class Character : Sprite
	{
		const int SPEED = 160;

		enum State
		{
			Standing,
			Walking,
			Ducking
		}

		State estadoActual = State.Standing;

		Vector2 posicionInicial = Vector2.Zero;
		Vector2 direccion = Vector2.Zero;
		Vector2 velocidad = Vector2.Zero;

		KeyboardState estadoTecladoAnterior;

		public void LoadContent(ContentManager theContentManager)
		{
			Position = new Vector2(0, 0);

			List<string> sprites = new List<string>();
			sprites.Add("normal");
			sprites.Add("ducking");
			base.LoadContent(theContentManager, sprites);

			Source = new Rectangle(0, 0, Source.Width, Source.Height);
		}

		public void Update(GameTime theGameTime)
		{
			KeyboardState estadoTecladoActual = Keyboard.GetState();

			UpdateMovement(estadoTecladoActual);
			UpdateDuck(estadoTecladoActual);

			estadoTecladoAnterior = estadoTecladoActual;

			base.Update(theGameTime, velocidad, direccion);
		}

		private void UpdateDuck(KeyboardState aCurrentKeyboardState)
		{
			if (aCurrentKeyboardState.IsKeyDown(Keys.RightShift) == true)
			{
				Duck();
			}
			else
			{
				StopDucking();
			}
		}

		private void Duck()
		{
			if (estadoActual == State.Standing)
			{
				velocidad = Vector2.Zero;
				direccion = Vector2.Zero;

				CurrentTexture = "ducking";
				estadoActual = State.Ducking;
			}
		}

		private void StopDucking()
		{
			if (estadoActual == State.Ducking)
			{
				CurrentTexture = "normal";
				estadoActual = State.Standing;
			}
		}


		private void UpdateMovement(KeyboardState aCurrentKeyboardState)
		{
			if (estadoActual == State.Standing)
			{
				velocidad = Vector2.Zero;
				direccion = Vector2.Zero;

				if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true)
				{
					velocidad.X = SPEED;
					direccion.X = -1;
				}
				else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) == true)
				{
					velocidad.X = SPEED;
					direccion.X = 1;
				}

				if (aCurrentKeyboardState.IsKeyDown(Keys.Up) == true)
				{
					velocidad.Y = SPEED;
					direccion.Y = -1;
				}
				else if (aCurrentKeyboardState.IsKeyDown(Keys.Down) == true)
				{
					velocidad.Y = SPEED;
					direccion.Y = 1;
				}
			}
		}
	}
}
